<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Models of ResultConverterGenerator</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../function_invocation_and_creation.html" title="Chapter 4. Function Invocation and Creation">
<link rel="prev" href="models_of_resultconverter.html" title="Models of ResultConverter">
<link rel="next" href="../to_from_python_type_conversion.html" title="Chapter 5. To/From Python Type Conversion">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="models_of_resultconverter.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../to_from_python_type_conversion.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat"></a><a class="link" href="models_of_resultconvertergenerat.html" title="Models of ResultConverterGenerator">Models
      of ResultConverterGenerator</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc">boost/python/copy_const_reference.hpp</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe">boost/python/copy_non_const_reference.hpp</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h">boost/python/manage_new_object.hpp</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_">boost/python/reference_existing_object.hpp</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp">boost/python/return_by_value.hpp</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point">boost/python/return_opaque_pointer.hpp</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc" title="boost/python/copy_const_reference.hpp">boost/python/copy_const_reference.hpp</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc.class_copy_const_reference">Class
          <code class="computeroutput"><span class="identifier">copy_const_reference</span></code></a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc.class_copy_const_reference_metaf">Class
          <code class="computeroutput"><span class="identifier">copy_const_reference</span></code> metafunctions</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc.class_copy_const_reference"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc.class_copy_const_reference" title="Class copy_const_reference">Class
          <code class="computeroutput"><span class="identifier">copy_const_reference</span></code></a>
</h4></div></div></div>
<p>
            <code class="computeroutput"><span class="identifier">copy_const_reference</span></code>
            is a model of <a class="link" href="../concepts/resultconverter.html#concepts.resultconverter.resultconvertergenerator_concept" title="ResultConverterGenerator Concept Requirements">ResultConverterGenerator</a>
            which can be used to wrap C++ functions returning a reference-to-const
            type such that the referenced value is copied into a new Python object.
          </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">struct</span> <span class="identifier">copy_const_reference</span>
    <span class="special">{</span>
        <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span><span class="special">;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc.class_copy_const_reference_metaf"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc.class_copy_const_reference_metaf" title="Class copy_const_reference metafunctions">Class
          <code class="computeroutput"><span class="identifier">copy_const_reference</span></code> metafunctions</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="identifier">T</span></code> is <code class="computeroutput"><span class="identifier">U</span> <span class="keyword">const</span><span class="special">&amp;</span></code> for some <code class="computeroutput"><span class="identifier">U</span></code>.
                </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="keyword">typedef</span> <span class="identifier">to_python_value</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">type</span><span class="special">;</span></code>
                </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc.example"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_const_referenc.example" title="Example">Example</a>
</h4></div></div></div>
<p>
            C++ module definition:
          </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">copy_const_reference</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_value_policy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// classes to wrap</span>
<span class="keyword">struct</span> <span class="identifier">Bar</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>

<span class="keyword">struct</span> <span class="identifier">Foo</span> <span class="special">{</span>
   <span class="identifier">Foo</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">:</span> <span class="special">{</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">x</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>
   <span class="identifier">Bar</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">get_bar</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">b</span><span class="special">;</span> <span class="special">}</span>
 <span class="keyword">private</span><span class="special">:</span>
   <span class="identifier">Bar</span> <span class="identifier">b</span><span class="special">;</span>
<span class="special">};</span>

<span class="comment">// Wrapper code</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">my_module</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Bar</span><span class="special">&gt;(</span><span class="string">"Bar"</span><span class="special">);</span>

     <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Foo</span><span class="special">&gt;(</span><span class="string">"Foo"</span><span class="special">,</span> <span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;())</span>
        <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"get_bar"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">Foo</span><span class="special">::</span><span class="identifier">get_bar</span>
            <span class="special">,</span> <span class="identifier">return_value_policy</span><span class="special">&lt;</span><span class="identifier">copy_const_reference</span><span class="special">&gt;())</span>
       <span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            Python code:
          </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">from</span> <span class="identifier">my_module</span> <span class="identifier">import</span> <span class="special">*</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">Foo</span><span class="special">(</span><span class="number">3</span><span class="special">)</span>         <span class="special">#</span> <span class="identifier">create</span> <span class="identifier">a</span> <span class="identifier">Foo</span> <span class="identifier">object</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">b</span> <span class="special">=</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">get_bar</span><span class="special">()</span>    <span class="special">#</span> <span class="identifier">make</span> <span class="identifier">a</span> <span class="identifier">copy</span> <span class="identifier">of</span> <span class="identifier">the</span> <span class="identifier">internal</span> <span class="identifier">Bar</span> <span class="identifier">object</span>
</pre>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe" title="boost/python/copy_non_const_reference.hpp">boost/python/copy_non_const_reference.hpp</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe.class_copy_non_const_reference">Class
          <code class="computeroutput"><span class="identifier">copy_non_const_reference</span></code></a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe.class_copy_non_const_reference_m">Class
          <code class="computeroutput"><span class="identifier">copy_non_const_reference</span></code>
          metafunctions</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe.class_copy_non_const_reference"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe.class_copy_non_const_reference" title="Class copy_non_const_reference">Class
          <code class="computeroutput"><span class="identifier">copy_non_const_reference</span></code></a>
</h4></div></div></div>
<p>
            <code class="computeroutput"><span class="identifier">copy_non_const_reference</span></code>
            is a model of <a class="link" href="../concepts/resultconverter.html#concepts.resultconverter.resultconvertergenerator_concept" title="ResultConverterGenerator Concept Requirements">ResultConverterGenerator</a>
            which can be used to wrap C++ functions returning a reference-to-non-const
            type such that the referenced value is copied into a new Python object.
          </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">struct</span> <span class="identifier">copy_non_const_reference</span>
    <span class="special">{</span>
        <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span><span class="special">;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe.class_copy_non_const_reference_m"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe.class_copy_non_const_reference_m" title="Class copy_non_const_reference metafunctions">Class
          <code class="computeroutput"><span class="identifier">copy_non_const_reference</span></code>
          metafunctions</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="identifier">T</span></code> is <code class="computeroutput"><span class="identifier">U</span> <span class="special">&amp;</span></code>
                  for some non-const <code class="computeroutput"><span class="identifier">U</span></code>.
                </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="keyword">typedef</span> <span class="identifier">to_python_value</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">type</span></code>;
                </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe.example"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_copy_non_const_refe.example" title="Example">Example</a>
</h4></div></div></div>
<p>
            C++ module definition:
          </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">copy_non_const_reference</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_value_policy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// classes to wrap</span>
<span class="keyword">struct</span> <span class="identifier">Bar</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>

<span class="keyword">struct</span> <span class="identifier">Foo</span> <span class="special">{</span>
   <span class="identifier">Foo</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">:</span> <span class="special">{</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">x</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>
   <span class="identifier">Bar</span><span class="special">&amp;</span> <span class="identifier">get_bar</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">b</span><span class="special">;</span> <span class="special">}</span>
 <span class="keyword">private</span><span class="special">:</span>
   <span class="identifier">Bar</span> <span class="identifier">b</span><span class="special">;</span>
<span class="special">};</span>

<span class="comment">// Wrapper code</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">my_module</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Bar</span><span class="special">&gt;(</span><span class="string">"Bar"</span><span class="special">);</span>

     <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Foo</span><span class="special">&gt;(</span><span class="string">"Foo"</span><span class="special">,</span> <span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;())</span>
        <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"get_bar"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">Foo</span><span class="special">::</span><span class="identifier">get_bar</span>
            <span class="special">,</span> <span class="identifier">return_value_policy</span><span class="special">&lt;</span><span class="identifier">copy_non_const_reference</span><span class="special">&gt;())</span>
       <span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            Python code:
          </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">from</span> <span class="identifier">my_module</span> <span class="identifier">import</span> <span class="special">*</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">Foo</span><span class="special">(</span><span class="number">3</span><span class="special">)</span>         <span class="special">#</span> <span class="identifier">create</span> <span class="identifier">a</span> <span class="identifier">Foo</span> <span class="identifier">object</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">b</span> <span class="special">=</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">get_bar</span><span class="special">()</span>    <span class="special">#</span> <span class="identifier">make</span> <span class="identifier">a</span> <span class="identifier">copy</span> <span class="identifier">of</span> <span class="identifier">the</span> <span class="identifier">internal</span> <span class="identifier">Bar</span> <span class="identifier">object</span>
</pre>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h" title="boost/python/manage_new_object.hpp">boost/python/manage_new_object.hpp</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h.class_manage_new_object">Class
          <code class="computeroutput"><span class="identifier">manage_new_object</span></code></a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h.class_manage_new_object_metafunc">Class
          <code class="computeroutput"><span class="identifier">manage_new_object</span></code> metafunctions</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h.class_manage_new_object"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h.class_manage_new_object" title="Class manage_new_object">Class
          <code class="computeroutput"><span class="identifier">manage_new_object</span></code></a>
</h4></div></div></div>
<p>
            <code class="computeroutput"><span class="identifier">manage_new_object</span></code> is
            a model of <a class="link" href="../concepts/resultconverter.html#concepts.resultconverter.resultconvertergenerator_concept" title="ResultConverterGenerator Concept Requirements">ResultConverterGenerator</a>
            which can be used to wrap C++ functions which return a pointer to an
            object allocated with a new-expression, and expect the caller to take
            responsibility for deleting that object.
          </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">struct</span> <span class="identifier">manage_new_object</span>
    <span class="special">{</span>
        <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span><span class="special">;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h.class_manage_new_object_metafunc"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h.class_manage_new_object_metafunc" title="Class manage_new_object metafunctions">Class
          <code class="computeroutput"><span class="identifier">manage_new_object</span></code> metafunctions</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="identifier">T</span></code> is <code class="computeroutput"><span class="identifier">U</span><span class="special">*</span></code>
                  for some <code class="computeroutput"><span class="identifier">U</span></code>.
                </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="keyword">typedef</span> <span class="identifier">to_python_indirect</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">type</span><span class="special">;</span></code>
                </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h.example"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_manage_new_object_h.example" title="Example">Example</a>
</h4></div></div></div>
<p>
            In C++:
          </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">manage_new_object</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_value_policy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>


<span class="keyword">struct</span> <span class="identifier">Foo</span> <span class="special">{</span>
   <span class="identifier">Foo</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">x</span><span class="special">){}</span>
   <span class="keyword">int</span> <span class="identifier">get_x</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>
   <span class="keyword">int</span> <span class="identifier">x</span><span class="special">;</span>
<span class="special">};</span>

<span class="identifier">Foo</span><span class="special">*</span> <span class="identifier">make_foo</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">new</span> <span class="identifier">Foo</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span> <span class="special">}</span>

<span class="comment">// Wrapper code</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">my_module</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">def</span><span class="special">(</span><span class="string">"make_foo"</span><span class="special">,</span> <span class="identifier">make_foo</span><span class="special">,</span> <span class="identifier">return_value_policy</span><span class="special">&lt;</span><span class="identifier">manage_new_object</span><span class="special">&gt;())</span>
    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Foo</span><span class="special">&gt;(</span><span class="string">"Foo"</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"get_x"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">Foo</span><span class="special">::</span><span class="identifier">get_x</span><span class="special">)</span>
        <span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            Python code:
          </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">from</span> <span class="identifier">my_module</span> <span class="identifier">import</span> <span class="special">*</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">make_foo</span><span class="special">(</span><span class="number">3</span><span class="special">)</span>     <span class="special">#</span> <span class="identifier">create</span> <span class="identifier">a</span> <span class="identifier">Foo</span> <span class="identifier">object</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">get_x</span><span class="special">()</span>
<span class="number">3</span>
</pre>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_" title="boost/python/reference_existing_object.hpp">boost/python/reference_existing_object.hpp</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_.class_reference_existing_object">Class
          <code class="computeroutput"><span class="identifier">reference_existing_object</span></code></a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_.class_reference_existing_object_">Class
          <code class="computeroutput"><span class="identifier">reference_existing_object</span></code>
          metafunctions</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_.class_reference_existing_object"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_.class_reference_existing_object" title="Class reference_existing_object">Class
          <code class="computeroutput"><span class="identifier">reference_existing_object</span></code></a>
</h4></div></div></div>
<p>
            <code class="computeroutput"><span class="identifier">reference_existing_object</span></code>
            is a model of <a class="link" href="../concepts/resultconverter.html#concepts.resultconverter.resultconvertergenerator_concept" title="ResultConverterGenerator Concept Requirements">ResultConverterGenerator</a>
            which can be used to wrap C++ functions which return a reference or pointer
            to a C++ object. When the wrapped function is called, the value referenced
            by its return value is not copied. A new Python object is created which
            contains a pointer to the referent, and no attempt is made to ensure
            that the lifetime of the referent is at least as long as that of the
            corresponding Python object. Thus, it can be <span class="bold"><strong>highly
            dangerous</strong></span> to use <code class="computeroutput"><span class="identifier">reference_existing_object</span></code>
            without additional lifetime management from such models of <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>
            as <a class="link" href="models_of_callpolicies.html#function_invocation_and_creation.models_of_callpolicies.boost_python_with_custodian_and_.class_with_custodian_and_ward" title="Class with_custodian_and_ward"><code class="computeroutput"><span class="identifier">with_custodian_and_ward</span></code></a>. This
            class is used in the implementation of <a class="link" href="models_of_callpolicies.html#function_invocation_and_creation.models_of_callpolicies.boost_python_return_internal_ref.class_template_return_internal_r" title="Class template return_internal_reference"><code class="computeroutput"><span class="identifier">return_internal_reference</span></code></a>.
          </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">struct</span> <span class="identifier">reference_existing_object</span>
    <span class="special">{</span>
        <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span><span class="special">;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_.class_reference_existing_object_"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_.class_reference_existing_object_" title="Class reference_existing_object metafunctions">Class
          <code class="computeroutput"><span class="identifier">reference_existing_object</span></code>
          metafunctions</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="identifier">T</span></code> is <code class="computeroutput"><span class="identifier">U</span><span class="special">&amp;</span></code>
                  or <code class="computeroutput"><span class="identifier">U</span><span class="special">*</span></code>
                  for some <code class="computeroutput"><span class="identifier">U</span></code>.
                </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="keyword">typedef</span> <span class="identifier">to_python_indirect</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">V</span><span class="special">&gt;</span> <span class="identifier">type</span><span class="special">;</span></code>, where V is a class whose static
                  execute function constructs an instance holder containing an unowned
                  <code class="computeroutput"><span class="identifier">U</span><span class="special">*</span></code>
                  pointing to the referent of the wrapped function's return value.
                </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_.example"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_reference_existing_.example" title="Example">Example</a>
</h4></div></div></div>
<p>
            In C++:
          </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">reference_existing_object</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_value_policy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">utility</span><span class="special">&gt;</span>

<span class="comment">// classes to wrap</span>
<span class="keyword">struct</span> <span class="identifier">Singleton</span>
<span class="special">{</span>
   <span class="identifier">Singleton</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">x</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> <span class="special">{}</span>

   <span class="keyword">int</span> <span class="identifier">exchange</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">)</span>  <span class="comment">// set x and return the old value</span>
   <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span><span class="special">(</span><span class="identifier">n</span><span class="special">,</span> <span class="identifier">x</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">n</span><span class="special">;</span>
   <span class="special">}</span>

   <span class="keyword">int</span> <span class="identifier">x</span><span class="special">;</span>
<span class="special">};</span>

<span class="identifier">Singleton</span><span class="special">&amp;</span> <span class="identifier">get_it</span><span class="special">()</span>
<span class="special">{</span>
   <span class="keyword">static</span> <span class="identifier">Singleton</span> <span class="identifier">just_one</span><span class="special">;</span>
   <span class="keyword">return</span> <span class="identifier">just_one</span><span class="special">;</span>
<span class="special">}</span>

<span class="comment">// Wrapper code</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">singleton</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">def</span><span class="special">(</span><span class="string">"get_it"</span><span class="special">,</span> <span class="identifier">get_it</span><span class="special">,</span>
        <span class="identifier">return_value_policy</span><span class="special">&lt;</span><span class="identifier">reference_existing_object</span><span class="special">&gt;());</span>

    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Singleton</span><span class="special">&gt;(</span><span class="string">"Singleton"</span><span class="special">)</span>
       <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"exchange"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">Singleton</span><span class="special">::</span><span class="identifier">exchange</span><span class="special">)</span>
       <span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            Python code:
          </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">import</span> <span class="identifier">singleton</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">s1</span> <span class="special">=</span> <span class="identifier">singleton</span><span class="special">.</span><span class="identifier">get_it</span><span class="special">()</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">s2</span> <span class="special">=</span> <span class="identifier">singleton</span><span class="special">.</span><span class="identifier">get_it</span><span class="special">()</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">id</span><span class="special">(</span><span class="identifier">s1</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">id</span><span class="special">(</span><span class="identifier">s2</span><span class="special">)</span>  <span class="special">#</span> <span class="identifier">s1</span> <span class="keyword">and</span> <span class="identifier">s2</span> <span class="identifier">are</span> <span class="keyword">not</span> <span class="identifier">the</span> <span class="identifier">same</span> <span class="identifier">object</span>
<span class="number">0</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">s1</span><span class="special">.</span><span class="identifier">exchange</span><span class="special">(</span><span class="number">42</span><span class="special">)</span>   <span class="special">#</span> <span class="identifier">but</span> <span class="identifier">they</span> <span class="identifier">reference</span> <span class="identifier">the</span> <span class="identifier">same</span> <span class="identifier">C</span><span class="special">++</span> <span class="identifier">Singleton</span>
<span class="number">0</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">s2</span><span class="special">.</span><span class="identifier">exchange</span><span class="special">(</span><span class="number">99</span><span class="special">)</span>
<span class="number">42</span>
</pre>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp" title="boost/python/return_by_value.hpp">boost/python/return_by_value.hpp</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp.class_return_by_value">Class
          <code class="computeroutput"><span class="identifier">return_by_value</span></code></a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp.class_return_by_value_metafuncti">Class
          <code class="computeroutput"><span class="identifier">return_by_value</span></code> metafunctions</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp.class_return_by_value"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp.class_return_by_value" title="Class return_by_value">Class
          <code class="computeroutput"><span class="identifier">return_by_value</span></code></a>
</h4></div></div></div>
<p>
            <code class="computeroutput"><span class="identifier">return_by_value</span></code> is a
            model of <a class="link" href="../concepts/resultconverter.html#concepts.resultconverter.resultconvertergenerator_concept" title="ResultConverterGenerator Concept Requirements">ResultConverterGenerator</a>
            which can be used to wrap C++ functions returning any reference or value
            type such that the return value is copied into a new Python object.
          </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">struct</span> <span class="identifier">return_by_value</span>
    <span class="special">{</span>
        <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span><span class="special">;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp.class_return_by_value_metafuncti"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp.class_return_by_value_metafuncti" title="Class return_by_value metafunctions">Class
          <code class="computeroutput"><span class="identifier">return_by_value</span></code> metafunctions</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="keyword">typedef</span> <span class="identifier">to_python_value</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">type</span><span class="special">;</span></code>
                </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp.example"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_by_value_hpp.example" title="Example">Example</a>
</h4></div></div></div>
<p>
            In C++:
          </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_by_value</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_value_policy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// classes to wrap</span>
<span class="keyword">struct</span> <span class="identifier">Bar</span> <span class="special">{</span> <span class="special">};</span>

<span class="identifier">Bar</span> <span class="identifier">global_bar</span><span class="special">;</span>

<span class="comment">// functions to wrap:</span>
<span class="identifier">Bar</span> <span class="identifier">b1</span><span class="special">();</span>
<span class="identifier">Bar</span><span class="special">&amp;</span> <span class="identifier">b2</span><span class="special">();</span>
<span class="identifier">Bar</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">b3</span><span class="special">();</span>

<span class="comment">// Wrapper code</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">def_void_function</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">R</span> <span class="special">(*</span><span class="identifier">f</span><span class="special">)())</span>
<span class="special">{</span>
   <span class="identifier">def</span><span class="special">(</span><span class="identifier">name</span><span class="special">,</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">return_value_policy</span><span class="special">&lt;</span><span class="identifier">return_by_value</span><span class="special">&gt;());</span>
<span class="special">}</span>

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">my_module</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Bar</span><span class="special">&gt;(</span><span class="string">"Bar"</span><span class="special">);</span>
    <span class="identifier">def_void_function</span><span class="special">(</span><span class="string">"b1"</span><span class="special">,</span> <span class="identifier">b1</span><span class="special">);</span>
    <span class="identifier">def_void_function</span><span class="special">(</span><span class="string">"b2"</span><span class="special">,</span> <span class="identifier">b2</span><span class="special">);</span>
    <span class="identifier">def_void_function</span><span class="special">(</span><span class="string">"b3"</span><span class="special">,</span> <span class="identifier">b3</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
            Python code:
          </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">from</span> <span class="identifier">my_module</span> <span class="identifier">import</span> <span class="special">*</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">b</span> <span class="special">=</span> <span class="identifier">b1</span><span class="special">()</span> <span class="special">#</span> <span class="identifier">each</span> <span class="identifier">of</span> <span class="identifier">these</span> <span class="identifier">calls</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">b</span> <span class="special">=</span> <span class="identifier">b2</span><span class="special">()</span> <span class="special">#</span> <span class="identifier">creates</span> <span class="identifier">a</span> <span class="identifier">brand</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">b</span> <span class="special">=</span> <span class="identifier">b3</span><span class="special">()</span> <span class="special">#</span> <span class="keyword">new</span> <span class="identifier">Bar</span> <span class="identifier">object</span>
</pre>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point" title="boost/python/return_opaque_pointer.hpp">boost/python/return_opaque_pointer.hpp</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point.class_return_opaqe_pointer">Class
          <code class="computeroutput"><span class="identifier">return_opaqe_pointer</span></code></a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point.class_return_opaque_pointer_meta">Class
          <code class="computeroutput"><span class="identifier">return_opaque_pointer</span></code> metafunctions</a></span></dt>
<dt><span class="section"><a href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point.class_return_opaqe_pointer"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point.class_return_opaqe_pointer" title="Class return_opaqe_pointer">Class
          <code class="computeroutput"><span class="identifier">return_opaqe_pointer</span></code></a>
</h4></div></div></div>
<p>
            return_opaque_pointer is a model of <a class="link" href="../concepts/resultconverter.html#concepts.resultconverter.resultconvertergenerator_concept" title="ResultConverterGenerator Concept Requirements">ResultConverterGenerator</a>
            which can be used to wrap C++ functions returning pointers to undefined
            types such that the return value is copied into a new Python object.
          </p>
<p>
            In addition to specifying the <code class="computeroutput"><span class="identifier">return_opaque_pointer</span></code>
            policy the <a class="link" href="../to_from_python_type_conversion/boost_python_opaque_pointer_conv.html#to_from_python_type_conversion.boost_python_opaque_pointer_conv.macro_boost_python_opaque_specia" title="Macro BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(Pointee)"><code class="computeroutput"><span class="identifier">BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID</span></code></a>
            macro must be used to define specializations for the <a class="link" href="../utility_and_infrastructure/boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.functions" title="Functions"><code class="computeroutput"><span class="identifier">type_id</span></code></a> function on the type
            pointed to by returned pointer.
          </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">struct</span> <span class="identifier">return_opaque_pointer</span>
    <span class="special">{</span>
        <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span><span class="special">;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point.class_return_opaque_pointer_meta"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point.class_return_opaque_pointer_meta" title="Class return_opaque_pointer metafunctions">Class
          <code class="computeroutput"><span class="identifier">return_opaque_pointer</span></code> metafunctions</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="identifier">detail</span><span class="special">::</span><span class="identifier">opaque_conversion_holder</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span>
                  <span class="identifier">type</span><span class="special">;</span></code>
                </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point.example"></a><a class="link" href="models_of_resultconvertergenerat.html#function_invocation_and_creation.models_of_resultconvertergenerat.boost_python_return_opaque_point.example" title="Example">Example</a>
</h4></div></div></div>
<p>
            In C++:
          </p>
<pre class="programlisting"><span class="preprocessor"># include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_opaque_pointer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor"># include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">def</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor"># include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor"># include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_value_policy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">typedef</span> <span class="keyword">struct</span> <span class="identifier">opaque_</span> <span class="special">*</span><span class="identifier">opaque</span><span class="special">;</span>

<span class="identifier">opaque</span> <span class="identifier">the_op</span>   <span class="special">=</span> <span class="special">((</span><span class="identifier">opaque</span><span class="special">)</span> <span class="number">0x47110815</span><span class="special">);</span>

<span class="identifier">opaque</span> <span class="identifier">get</span> <span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">the_op</span><span class="special">;</span> <span class="special">}</span>
<span class="keyword">void</span> <span class="identifier">use</span> <span class="special">(</span><span class="identifier">opaque</span> <span class="identifier">op</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">if</span> <span class="special">(</span><span class="identifier">op</span> <span class="special">!=</span> <span class="identifier">the_op</span><span class="special">)</span>
	<span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">(</span><span class="string">"failed"</span><span class="special">));</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">failuse</span> <span class="special">(</span><span class="identifier">opaque</span> <span class="identifier">op</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">if</span> <span class="special">(</span><span class="identifier">op</span> <span class="special">==</span> <span class="identifier">the_op</span><span class="special">)</span>
	<span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">(</span><span class="string">"success"</span><span class="special">));</span>
<span class="special">}</span>

<span class="identifier">BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID</span><span class="special">(</span><span class="identifier">opaque_</span><span class="special">)</span>

<span class="keyword">namespace</span> <span class="identifier">bpl</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">opaque_ext</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">bpl</span><span class="special">::</span><span class="identifier">def</span> <span class="special">(</span>
        <span class="string">"get"</span><span class="special">,</span> <span class="special">&amp;::</span><span class="identifier">get</span><span class="special">,</span> <span class="identifier">bpl</span><span class="special">::</span><span class="identifier">return_value_policy</span><span class="special">&lt;</span><span class="identifier">bpl</span><span class="special">::</span><span class="identifier">return_opaque_pointer</span><span class="special">&gt;());</span>
    <span class="identifier">bpl</span><span class="special">::</span><span class="identifier">def</span> <span class="special">(</span><span class="string">"use"</span><span class="special">,</span> <span class="special">&amp;::</span><span class="identifier">use</span><span class="special">);</span>
    <span class="identifier">bpl</span><span class="special">::</span><span class="identifier">def</span> <span class="special">(</span><span class="string">"failuse"</span><span class="special">,</span> <span class="special">&amp;::</span><span class="identifier">failuse</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
            Python code:
          </p>
<pre class="programlisting"><span class="string">""</span><span class="string">"
&gt;&gt;&gt; from opaque_ext import *
&gt;&gt;&gt; #
&gt;&gt;&gt; # Check for correct conversion
&gt;&gt;&gt; use(get())
&gt;&gt;&gt; failuse(get())
Traceback (most recent call last):
        ...
RuntimeError: success
&gt;&gt;&gt; #
&gt;&gt;&gt; # Check that there is no conversion from integers ...
&gt;&gt;&gt; use(0)
Traceback (most recent call last):
        ...
TypeError: bad argument type for built-in operation
&gt;&gt;&gt; #
&gt;&gt;&gt; # ... and from strings to opaque objects
&gt;&gt;&gt; use("</span><span class="string">")
Traceback (most recent call last):
        ...
TypeError: bad argument type for built-in operation
"</span><span class="string">""</span>
<span class="identifier">def</span> <span class="identifier">run</span><span class="special">(</span><span class="identifier">args</span> <span class="special">=</span> <span class="identifier">None</span><span class="special">):</span>
    <span class="identifier">import</span> <span class="identifier">sys</span>
    <span class="identifier">import</span> <span class="identifier">doctest</span>

    <span class="keyword">if</span> <span class="identifier">args</span> <span class="identifier">is</span> <span class="keyword">not</span> <span class="identifier">None</span><span class="special">:</span>
        <span class="identifier">sys</span><span class="special">.</span><span class="identifier">argv</span> <span class="special">=</span> <span class="identifier">args</span>
    <span class="keyword">return</span> <span class="identifier">doctest</span><span class="special">.</span><span class="identifier">testmod</span><span class="special">(</span><span class="identifier">sys</span><span class="special">.</span><span class="identifier">modules</span><span class="special">.</span><span class="identifier">get</span><span class="special">(</span><span class="identifier">__name__</span><span class="special">))</span>

<span class="keyword">if</span> <span class="identifier">__name__</span> <span class="special">==</span> <span class="char">'__main__'</span><span class="special">:</span>
    <span class="identifier">print</span> <span class="string">"running..."</span>
    <span class="identifier">import</span> <span class="identifier">sys</span>
    <span class="identifier">sys</span><span class="special">.</span><span class="identifier">exit</span><span class="special">(</span><span class="identifier">run</span><span class="special">()[</span><span class="number">0</span><span class="special">])</span>
</pre>
</div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="models_of_resultconverter.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../to_from_python_type_conversion.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
